/**
 * i18n, 美版的mp3 download栏目, 共用js
 */
$(function() {
	mp3_index.run();
});

var mp3_index = {
	run : function() {
		this._init_();
		this._start_();
		this._init_events();
		this.contextPath = $("#contextPath").val() || '';		
	},

	_init_ : function() {

	},

	_start_ : function() {

	},

	_init_events : function() {
		$("#mprDownload_search").smenu({
			backend : 'fshared',
			defaultValue : 'Search for Music...',
			onchange : function(query) {
				var sreg = /[`~!@#\$%\^&\*\(\)\+=\[\{\]\};:\<\>\?\/\\\|"]+/g;
				query = query.replace(sreg, "");
				location.href = mp3_index.contextPath + "/mg/mp3/" + query;
			}
		});

		$("#page_box").pager({
			change : function(page) {
				mp3_index._list_music(page);
			}
		});
	},

	_list_music : function(page) {
		var url = mp3_index.contextPath + "/mg/mp3/list/" + page + "?ajax=true";
		$.get(url, function(result) {
			var $div = $("<div>" + result + "</div>");
			mp3_index._shorten_music_title($div);
			$("#mp3_list_div").html($div.html());
		});
	},

	_shorten_music_title : function($div) {
		$("ul > li > a", $div).each(function() {
			var $this = $(this);
			var txt = $this.text().replace('mp3', '');
			if (txt.length >= 30) {
				txt = txt.substring(0, 25) + "...";
				$this.html(txt + " <span>mp3</span>");
			}
		});
	}
};